package com.android.tiango.baba.act;

import java.io.File;
import java.util.ArrayList;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseApplication;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.AddressModel;
import com.android.tiango.baba.entity.MessageEnity;
import com.android.tiango.baba.entity.MessageModel;
import com.android.tiango.baba.entity.OneAddressEnity;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.PullListView.PullListViewListener;

public class ActMessageCenter extends BaListActivity<MessageModel> implements
		PullListViewListener {

	LayoutInflater mInflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		setTitleName("消息中心");
		if (null != mTitleBar) {
			mTitleBar.setBackIcon(R.drawable.back);
			setBackOnClickListener();
		}
		mImageLoader = new ImageLoader(this, R.drawable.icon_bar_photo);
		mInflater = getLayoutInflater();
		mListView.setDivider(getResources().getDrawable(R.drawable.ic_line));

		mListView.setPullListener(this);
		mListView.startOnRefresh();
		
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_message_center, null);
			viewholder = new ViewHolder();

			viewholder.message_iv_icon = (ImageView) convertView
					.findViewById(R.id.iv_icon);
			viewholder.message_tv = (TextView) convertView
					.findViewById(R.id.tv_message);
			viewholder.sex = (ImageView) convertView.findViewById(R.id.iv_sex);
			viewholder.message_ll = (LinearLayout) convertView
					.findViewById(R.id.ll_message);
			viewholder.name = (TextView) convertView.findViewById(R.id.tv_name);
			viewholder.delete = (ImageView) convertView
					.findViewById(R.id.iv_delete);
			viewholder.guanfang = (ImageView) convertView
					.findViewById(R.id.iv_guanfang);
			viewholder.message_iv_focus = (ImageView) convertView
					.findViewById(R.id.iv_point);
			viewholder.time = (TextView) convertView.findViewById(R.id.tv_time);
			LayoutParams lp = viewholder.message_ll.getLayoutParams();
			lp.width = BaBaseApplication.mWidth;

			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		MessageModel bean = mAdapter.getItem(position);
		String headImg = bean.getHeadImg();
		String useSex = bean.getUserSex();
		String nickName = bean.getNickName();
		String content = bean.getContent();
		String isRead = bean.getIsRead();
		String msgType = bean.getMsgType();
		String floorSort = bean.getFloorSort();
		String createTime = bean.getCreateTime();

		mImageLoader.loadImage(headImg, viewholder.message_iv_icon);

		viewholder.name.setText(nickName);
		viewholder.message_tv.setText(content);
		viewholder.time.setText(TimeToUtil.getMessageDayFormat(Long
				.valueOf(createTime)));

		if ("1".equals(useSex)) {
			viewholder.sex.setImageResource(R.drawable.bg_sex_girl);
		} else {
			viewholder.sex.setImageResource(R.drawable.bg_sex);
		}
		if ("1".equals(msgType)) {
			viewholder.guanfang.setImageResource(R.drawable.bg_thelord);
		} else if ("2".equals(msgType)) {
			viewholder.guanfang.setImageResource(R.drawable.bg_guanfang);
		} else if ("3".equals(msgType)) {
			viewholder.guanfang.setImageResource(-1);
		}
		if ("0".equals(isRead)) {
			viewholder.message_iv_focus.setVisibility(View.VISIBLE);
		} else if ("1".equals(isRead)) {
			viewholder.message_iv_focus.setVisibility(View.INVISIBLE);
		}
		viewholder.delete.setTag(bean);
		viewholder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MessageModel newbean = (MessageModel) v.getTag();
				String messageId = newbean.getMessageId();
				String msgType = newbean.getMsgType();

				String fromUserId = newbean.getFromUserId();
				deleteMessage(messageId, msgType, fromUserId);
				showWaitingDialog();
				ToastUtils.showMessage("点击了删除按钮！！！");
			}
		});
		viewholder.message_ll.setTag(bean);
		viewholder.message_ll.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MessageModel newbean = (MessageModel) arg0.getTag();
				if (null != newbean) {
					if ("2".equals(newbean.getMsgType())) {
						Intent intent = new Intent(ActMessageCenter.this,
								ActOfficialNews.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable(Constants.DATA, newbean);
						intent.putExtras(bundle);
						startActivity(intent);
					}
				}
			}
		});

		return convertView;
	}

	private void deleteMessage(String messageId, String msgType,
			String fromUserId) {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("token", ShareCookie.getToken());// taken
		httpparams.put("messageId", messageId);// taken
		httpparams.put("msgType", msgType);// taken
		httpparams.put("fromUserId", fromUserId);// taken

		client.post(HttpUrl.getUrl(HttpUrl.DELETEMESSAGE), httpparams,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("删除成功！");

					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("获取失败！===" + arg1);

					}
				});//

	}

	static class ViewHolder {
		private TextView message_tv, name, time;
		private LinearLayout message_ll;
		private ImageView message_iv_focus, sex;
		private ImageView message_iv_icon, guanfang;
		private LinearLayout message_image;

		private HorizontalScrollView message_hs;
		private ImageView delete;

	}

	@Override
	public void onPullRefresh() {
		// TODO Auto-generated method stub
		mPageIndex = 1;
		loadMessage();
	}

	@Override
	public void onPullLoadMore() {
		// TODO Auto-generated method stub
		mPageIndex += 1;
		loadMessage();
	}

	private void loadMessage() {
		// TODO Auto-generated method stub
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams httpparams = new HttpParams();
		httpparams.put("token", ShareCookie.getToken());// taken

		client.get(HttpUrl.getUrl(HttpUrl.MESSAGE), httpparams,
				new HttpCallBack() {
					@Override
					public void onHttpSuccess(Object arg0) {
						// TODO Auto-generated method stub
						MessageEnity messageenity = (MessageEnity) arg0;
						if (null != messageenity) {
							mAdapter.putData(messageenity.getData());
						}
						ToastUtils.showMessage("成功获取！");
						mListView.onRefreshFinish();
						mListView.onLoadFinish();
					}

					@Override
					public void onHttpStarted() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onHttpFailure(Exception arg0, String arg1) {
						// TODO Auto-generated method stub
						dismissWaitingDialog();
						ToastUtils.showMessage("获取失败！===" + arg1);
						if (mPageIndex > 1) {
							mPageIndex -= 1;
						}
						arg0.printStackTrace();

						mListView.onRefreshFinish();
						mListView.onLoadFinish();
					}
				}, MessageEnity.class);//
	}
}
